package handlres

import (
	"1-14zk/api/request"
	"1-14zk/api/responres"
	"1-14zk/dao"
	"1-14zk/models"
	"github.com/gin-gonic/gin"
)

func ArticleAdd(c *gin.Context) {
	var data request.ArticleAdd
	if err := c.ShouldBind(&data); err != nil {
		responres.ResponResDataError(c, err.Error())
		return
	}
	article := models.Article{
		UserId:  data.UserId,
		Title:   data.Title,
		Intro:   data.Intro,
		FileUrl: data.FileUrl,
	}
	err := dao.ArticleAdd(&article)
	if err != nil {
		responres.ResponResDataError(c, "添加失败")
		return
	}
	responres.ResponResDataSuccess(c, article.ID)
}

func ArticleGood(c *gin.Context) {
	var data request.ArticleGood
	if err := c.ShouldBind(&data); err != nil {
		responres.ResponResDataError(c, err.Error())
		return
	}
	good := models.ArticleGood{
		UserId:    data.UserId,
		ArticleId: data.ArticleId,
		Status:    1,
	}
	err := dao.ArticleGood(&good)
	if err != nil {
		responres.ResponResDataError(c, "点赞失败")
		return
	}
	responres.ResponResDataSuccess(c, good)
}

func ArticleBrowse(c *gin.Context) {
	var data request.ArticleBrowse
	if err := c.ShouldBind(&data); err != nil {
		responres.ResponResDataError(c, err.Error())
		return
	}
	good := models.ArticleGood{
		UserId:    data.UserId,
		ArticleId: data.ArticleId,
		Status:    2,
	}
	err := dao.ArticleBrowse(&good)
	if err != nil {
		responres.ResponResDataError(c, "浏览失败")
		return
	}
	responres.ResponResDataSuccess(c, good)
}

func ArticleReview(c *gin.Context) {
	var data request.ArticleReview
	if err := c.ShouldBind(&data); err != nil {
		responres.ResponResDataError(c, err.Error())
		return
	}
	review := models.ArticleReview{
		UserId:    data.UserId,
		ArticleId: data.ArticleId,
		Context:   data.Context,
		FathId:    0,
	}
	err := dao.ArticleReview(&review)
	if err != nil {
		responres.ResponResDataError(c, "评论失败")
		return
	}
	responres.ResponResDataSuccess(c, review)
}

func ResArticleReview(c *gin.Context) {
	var data request.ResArticleReview
	if err := c.ShouldBind(&data); err != nil {
		responres.ResponResDataError(c, err.Error())
		return
	}
	review := models.ArticleReview{
		UserId:    data.UserId,
		ArticleId: data.ArticleId,
		Context:   data.Context,
		FathId:    1,
	}
	err := dao.ResArticleReview(&review)
	if err != nil {
		responres.ResponResDataError(c, "评论失败")
		return
	}
	responres.ResponResDataSuccess(c, review)
}

func ReviewGood(c *gin.Context) {
	var data request.ReviewGood
	if err := c.ShouldBind(&data); err != nil {
		responres.ResponResDataError(c, err.Error())
		return
	}
	Reviewgood := models.ReviewGood{
		UserId:    data.UserId,
		ArticleId: data.ArticleId,
		ReviewId:  data.ReviewId,
	}
	err := dao.ReviewGood(&Reviewgood)
	if err != nil {
		responres.ResponResDataError(c, "评论点赞失败")
		return
	}
	responres.ResponResDataSuccess(c, Reviewgood)
}

func DelReview(c *gin.Context) {
	var data request.DelReview
	if err := c.ShouldBind(&data); err != nil {
		responres.ResponResDataError(c, err.Error())
		return
	}
	_, err := dao.DelReview(data.Id)
	if err != nil {
		responres.ResponResDataError(c, "删除失败")
		return
	}
	responres.ResponResDataSuccess(c, "删除成功")
}

func DelReviewGood(c *gin.Context) {
	var data request.DelReviewGood
	if err := c.ShouldBind(&data); err != nil {
		responres.ResponResDataError(c, err.Error())
		return
	}
	_, err := dao.DelReviewGood(data.Id)
	if err != nil {
		responres.ResponResDataError(c, "取消喜欢失败")
		return
	}
	responres.ResponResDataSuccess(c, "取消喜欢成功")
}
